/**
 * Copyright (c) 2014-now, secondriver (secondriver@yeah.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package secondriver.jfinal.rack.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import secondriver.jfinal.rack.kit.RockProperitesKit;
import secondriver.jfinal.rack.kit.RockSysValKit;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.StrKit;

/**
 * SystenValues from jfinal.properties configuration
 * 
 * @author ZhangXiao
 *
 */
public final class SystemValues {

	public SystemValues add(Properties properties) {
		if (StrKit.notNull(properties)) {
			this.store(properties);
		}
		return this;
	}

	public SystemValues add(String file) {
		Properties p = this.loadPropertyFile(file);
		this.add(p);
		return this;
	}

	public int size() {
		return RockSysValKit.configSize();
	}

	private Properties loadPropertyFile(String file) {
		Properties properties = new Properties();
		if (StrKit.isBlank(file)) {
			throw new IllegalArgumentException(
					"Parameter of file can not be blank");
		}
		if (file.contains("..")) {
			throw new IllegalArgumentException(
					"Parameter of file can not contains \"..\"");
		}
		InputStream inputStream = null;
		String fullFile; // String fullFile = PathUtil.getWebRootPath() + file;
		if (file.startsWith(File.separator)) {
			fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF"
					+ file;
		} else {
			fullFile = PathKit.getWebRootPath() + File.separator + "WEB-INF"
					+ File.separator + file;
		}
		try {
			File propertiesFile = new File(fullFile);
			if (!propertiesFile.exists()) {
				properties = RockProperitesKit.getDefaultProperty(file);
			} else {
				inputStream = new FileInputStream(new File(fullFile));
				properties.load(inputStream);
			}
		} catch (FileNotFoundException e) {
			throw new IllegalArgumentException("Properties file not found: "
					+ fullFile);
		} catch (IOException e) {
			throw new IllegalArgumentException(
					"Properties file can not be loading: " + fullFile);
		} finally {
			try {
				if (inputStream != null)
					inputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (properties == null)
			throw new RuntimeException("Properties file loading failed: "
					+ fullFile);
		return properties;
	}

	private void store(Properties properties) {
		if (null != properties && 0 != properties.size()) {
			for (Iterator<Entry<Object, Object>> iter = properties.entrySet()
					.iterator(); iter.hasNext();) {
				Entry<?, ?> entry = iter.next();
				RockSysValKit.add(String.valueOf(entry.getKey()),
						String.valueOf(entry.getValue()));
			}
		}
	}

}
